Unable to generate selected data details in tab created using shiny - r

I have started practicing shiny package for making dashboard, and i am still an amateur at R, please help me to display the data which will be selected using selectinput in the allocated tab which i have created for display of data.
I shall share my ui code as well as server code. Please assist how to display selected data in the data tab created.
ui.R code
library(shiny)
library(shinydashboard)
shinyUI(fluidPage(
titlePanel(h1("Test for application of all the tutorials completed till now")),
sidebarLayout(
sidebarPanel((h2("Information Panel Enter")),
selectInput("data", "Select the dataset for hist analysis",
choices = c("iris","pressure","USArrests", selected = "pressure")),
numericInput("obs", "Select the number of observations for the dataset", value = 5,min = 5,max = 30,step = 1 ),
sliderInput("bins", "Select the number of bins for histogram", value = 6, min = 6, max = 20, step = 1),
radioButtons("color", "selecct the color of histogram" , choices = c("black","purple","brown"))),
mainPanel((h3("Main Panel of all the information display")),
tabsetPanel(type = c("pills"),
tabPanel("Summary" , h4(textOutput("Mysumhead")) ,verbatimTextOutput("Mysum")),
tabPanel("Structure and Observation" , h4(textOutput("Mystrhead")), verbatimTextOutput("Mystr")),
tabPanel("Plot"),
tabPanel("Data" , verbatimTextOutput("Mydata"))))
)))
server.R code
library(shiny)
library(shinydashboard)
library(datasets)
shinyServer(function(input,output){
output$Mysum <- renderPrint({
summary(get(input$data))
})
output$Mysumhead <- renderText({
paste("Data Selected for checking summary is " , input$data)
})
output$Mystr <- renderPrint({
str(get(input$data))
})
output$Mystrhead <- renderText({
paste("Data selected for observing summary of the data is " , input$data)
})
output$Mydata <- renderTable({
data(input$data)
})
})

you are good in all point except one.
In the UI.R, in Data TAB just change to tableOutput("Mydata") and in Server.R change the code inside rendertable({}) change it to get(input$data).
It will be good to go. You should use tableOutput for displaying Table when you want to use renderTable in server side

Related

how to create a Histogram from dataframe based on a dropdown in SHINY

[picture of my code and shiny app][1]I am trying to write a shiny app where I have two drop down menus and I create two histograms from those dropdowns and a salary variable I have stored in a dataframe. I can create the drop downs but I am lost on how to save the selection and use the selection as a independent variable for my model. any help at all would be huge.
I tried using a save button to save the selection from the drop down but I couldn't get that to store the variable in a way that I could verify with my dataframe.
code below
library(dplyr)
library(ggplot2)
library(shiny)
data <- read.csv("C:/Users/lewis/OneDrive/Desktop/STA 580( R
programming)/Final_Project/salaries_entry.csv")
data
wxdata <- data.frame(
Remote = c("0%", "50%", "100%"),
Size = c("Small","Medium","Large")
)
remotelist <- unique(wxdata$Remote)
sizelist <- unique(wxdata$Size)
ui <- fluidPage(
titlePanel("Data Science / Data Engineer Salaries"),
sidebarLayout(
sidebarPanel(),
mainPanel(
h4("The purpose of this app is to give you an idea"),
h4("about the potential money you could be making as an"),
h4("entry level employee in the Data Science field based "),
h4("on a few key factors. Test it out and Get that BAG!")
)
),
inputPanel(
selectInput(
"RemoteWork",
label = "Select Amount of Remote Work",
choices = remotelist
)
),
inputPanel(
selectInput(
"CompanySize",
label = "Select the Size of the Company",
choices = sizelist
)
),
)
server <- function(input, output) {
output$minplot <- renderPlot(draw_plot(input$PlotCity))
}
shinyApp(ui = ui, server = server)

Update dataframe based on shiny widgets' inputs

Im trying to create a dataframe which will update its values based on the shiny widgets selections in the sidebar. But the datatable I use to check this does not seem to display all column names and cell values .
library(shiny)
library(dplyr)
library(shinydashboard)
library(DT)
### user inter phase
ui <- fluidPage(
### App title ----
titlePanel(title=div(img(src="pics/IRP_NHSc.jpg", width="99%")))
,
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
### Input files ----
selectInput("Price","Select the price for analysis", c("List price","End consumer price without VAT", "End consumer price with VAT", "Reimbursed price"),multiple = T),
selectInput("IRP_A","Select IRP formula", c("Price average"="PA","Median price"="MP","3 lowest price average"="3L", "Lowest price"="LP", "Turkish rule"="TR", "Swiss rule"="SR" )),
# Input: Slider for the number of bins ----
sliderInput(inputId = "increase",
label = "% increase",
min = -20,
max = 100,
value = 35), width = 2,
),
### Main panel for displaying outputs ----
mainPanel(
tabsetPanel(
tabPanel("Export report",
dataTableOutput("tab7"))
)
)
)
)
#### Server
server <- function(input, output, session) {
output$tab7<-renderDataTable({
Price<-input$Price
IRP<-input$IRP_A
Per<-input$increase
df<-as.data.frame(Price,IRP,Per)
})
}
shinyApp(ui = ui, server = server)
I'm not really sure what you want to see but I think as.data.frame is taking only the first object as the thing to convert into a data frame and the second as the row name. If you put them in a list you can see all the inputs in the table:
df<-as.data.frame(list('price' = Price, 'irp' = IRP,'per' = Per))
(although it does give an error until you select at least one price)

Failure to display DataTable in RShiny App

I'm hoping to display a reactive datatable for my Shiny app. I'm using renderDataTable() and have made sure that the data table is returned in the reactive function. I've noticed that the datatable renders fine outside of the Shiny App, so not a variable/computation error. The reason I'm hoping to use a datatable in the first place is so that I can display cleaned up column names and display the dataframe in a more clean manner. Please let me know what else I can try, or if I should change my approach.
Here is the server code:
server <- function(input, output) {
dataset <- reactive({
shiny_tuition_salary <- datatable(df_tuition_salary %>%
select(name, mean_net_cost, state) %>%
filter(mean_net_cost >= input$input_budget[1],
mean_net_cost <= input$input_budget[2],
state == input$input_state) %>%
select(name, mean_net_cost))
return(shiny_tuition_salary)
})
output$df <- renderDataTable({
dataset()
})
}
Currently nothing is displayed under the Table tab in the app. The app also successfully displays the table when DataTable is not used at all (i.e. removing datatable() from the server and using RenderTable instead of RenderDataTable), so I'm positive there's an issue with my implementation of RenderDataTable()
Thanks!
EDIT: Here's the ui code and a sample df_tuition_salary as well
ui <- fluidPage(
titlePanel("What colleges match your budget and rank?"),
# Sidebar laayout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "input_budget",
label = "Budget:",
min = 0,
max = 50000,
value = c(0, 15000)),
selectInput(inputId = "input_state",
label = "State (limited data, may limit options):",
choices = df_tuition_salary$state)
),
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(
id = 'output_df',
# tabPanel("Plot", plotOutput("plot")),
tabPanel("Table", tableOutput("df"))
)
)
)
)
df_tuition_salary:
df_tuition_salary <- data.frame(name = c("Aaniiih Nakoda College", "Abilene Christian University"),
mean_net_cost = c(7508.2414, 24884.0828),
state = c("N/A", "N/A"))
use dataTableOutput() function.
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(
id = 'output_df',
# tabPanel("Plot", plotOutput("plot")),
tabPanel("Table", dataTableOutput("df"))
)

R shiny Multiple slider inputs based on checkbox inputs

I have used the below code to create checkbox from my data.I would like to create slider input for each checkbox I select from the list.For example if the checkbox has 4 variables like "sky","earth","water","fire" and if I select sky, it should dynamically open a slider input for sky and if I select water it should open up one more slider input for water. I tried conditionalPanel,but I have more than 50 variables in my checkbox,so i cannot write condition for all the 50 variables. Is there any generalized method available in shiny?
server
output$choosedigital=renderUI({
if(is.null(bk$variables))
return()
checkboxGroupInput("choosemedia", "Choose digital",
choices = bk$variables,
selected = bk$variables)
})
output$test <- renderUI({
LL <- list(rep(0,length(input$choosedigital)))
for(i in 0:(length(input$choosedigital))) {
LL[i] <- list(sliderInput(inputId = paste(input$choosedigital,i)
, label = paste(input$choosedigital,i),
min=0,max=25,value = 5))
}
return(LL)
})
You want to put your sliderInputs inside a conditionalPanel in the UI and set the condition so that when the relevant checkbox is clicked the the condition equates to TRUE.
e.g.
library(shiny)
myData = c("One", "Two", "Three")
ui <- fluidPage(
checkboxGroupInput("choosemedia", "Choose digital",
choices = myData,
selected = myData),
textOutput("myText"),
conditionalPanel(condition = "input.choosemedia.includes('One')",
sliderInput("sliderOne", "Choose your value", min=0, max=100, value=50)
),
conditionalPanel(condition = "input.choosemedia.includes('Two')",
sliderInput("sliderTwo", "Choose your other value",
min=0, max=50, value=25))
)
# Define server logic
server <- function(input, output) {
output$myText <- renderText({input$choosemedia})
}
# Run the application
shinyApp(ui = ui, server = server)
If long as you know what the content of bk$variables is you can hardcode them, otherwise you'll have to generate these on the fly.
Hope this is enough info to get you going.

Dynamic UI - SelectInput Not Showing

Thank you all in advance for your help.
I'm still relatively new to R-Shiny and I'm trying to explore dynamic UI. I've done some research into renderUI, reactive, and observe functions but I'm still not sure of the right way to go about doing what I'm trying to accomplish which is:
User selects Y - dynamically populates choices based on loaded data in global.r
User selects X - dynamically populates choices based on loaded data in global.r
R plots the two
My problem is that the select inputs do not display - that makes sense to me given they are reactive. How do I show those select inputs when the page loads?
ui.R
require(shiny)
shinyUI(fluidPage(
tabPanel("Pineapple",
tabPanel("",
sidebarLayout(
sidebarPanel(
helpText("Choose X and Y"),
uiOutput("pineapple.pick_Y"),
uiOutput("pineapple.pick_X"),
),
mainPanel(
plotOutput("pineapple.plot")
)
)
)
)
))
server.R
shinyServer(function(input, output, session){
output$pineapple.plot <- renderPlot({
df <- data.frame(blue = c(1,2,3),
red = c(4,5,6),
boy = c("steve","steve","bill"),
girl = c("stacey","lauren","stacey")
plot(input$pineapple.pick_X,input$pineapple.pick_Y,data=df)
})
output$pineapple.pick_X <- renderUI({
selectInput("pineapple.x", label = h6("Select X"),
choices = c("blue","red"),
selected = "blue")
})
output$pineapple.pick_Y <- renderUI({
selectInput("pineapple.y", label = h6("Select Y"),
choices = c("boy","girl"),
selected = "boy")
})
})
Found a way around it - There wasn't a need to a dynamic UI really, just used variables I stored in global.R to pull the choices.
Thanks!

Resources