I am attempting to custom build a user-interface regression tool in R shiny for practice (i.e. my own version of spss for my general use-cases). I'm having trouble with the critical step of generating a regression formula from the user-uploaded dataset. I want the user to be able to select a dependent variable from a dropdown menu (and eventually to turn those generated variables into a formula in my server code).
I have tried to use a textOutput(names(userdata())) within the choices argument for a selectInput() function so that the user can select which of their variables should be a dependent variable once they have uploaded their dataset. However, this generates a list of properties of the dataset rather than the names of the columns themselves.
I've looked into other uses of reactive datasets that others have done but no one seems to have done precisely what I am trying to do or I am searching for them badly. (It seems like the most common probable use-case for Shiny, so I can't imagine how no one has figured this out yet, but I can't find anything)
library(shiny)
library(wired)
ui <- fluidPage(
sidebarLayout(sidebarPanel(
fileInput("FileInput", "Input Your Data Set (Must be .csv)"),
wired_select(inputId = "responsevar",
label = "Dependent Varibale:",
choices = textOutput(outputId = "variable_names")
)
), #sidebar panel
mainPanel(
tabsetPanel(
tabPanel("Table",
DT::dataTableOutput("table")
)
) #tabset Panel
) #main panel
) #sidebarlayout
) #fluidpage
server <- function(input, output, session) {
datasetInput <- reactive({
infile <- input$FileInput
if (is.null(infile))
return(NULL)
read.csv(infile$datapath, header = TRUE)
})
output$table = DT::renderDataTable(datasetInput())
output$variable_names <- reactive({
if (is.null(datasetInput()))
return(NULL)
names(datasetInput())
})
} #server
shinyApp(ui = ui, server = server)
textOutput is used to output text to the Shiny UI. This is includes generating appropriate HTML. As wired_select(..., choices = ???) is expecting an R object rather than HTML code this is unlikely to work.
One approach that is likely to work is using updateSelectInput. I do not know whether this has an equivalent function with the wired library, but in base shiny I would:
Initialise the selectInput with no choices
Update the choices in the drop down once the data has been selected
Try the following:
library(shiny)
ui <- fluidPage(
sidebarLayout(sidebarPanel(
fileInput("FileInput", "Input Your Data Set (Must be .csv)"),
selectInput(inputId = "responsevar",
label = "Dependent Varibale:",
choices = NULL)
), #sidebar panel
mainPanel(
tabsetPanel(
tabPanel("Table",
DT::dataTableOutput("table")
)
) #tabset Panel
) #main panel
) #sidebarlayout
) #fluidpage
server <- function(input, output, session) {
datasetInput <- reactive({
infile <- input$FileInput
if (is.null(infile))
return(NULL)
read.csv(infile$datapath, header = TRUE)
})
output$table = DT::renderDataTable(datasetInput())
observeEvent(datasetInput(),{
updateSelectInput(session, "responsevar", choices = names(datasetInput()))
})
} #server
shinyApp(ui = ui, server = server)
Hmm... as the above does not work with the wired library, I'll suggest another possible approach. (I can't install wired in my environment, so apologies if this is no better).
The idea here is to make the selector part of a dynamic R object (a UI object). Then if a file gets loaded, the UI object, which depends on the file, will also update.
library(shiny)
library(wired)
ui <- fluidPage(
sidebarLayout(sidebarPanel(
fileInput("FileInput", "Input Your Data Set (Must be .csv)"),
uiOutput("selector")
), #sidebar panel
mainPanel(
tabsetPanel(
tabPanel("Table",
DT::dataTableOutput("table")
)
) #tabset Panel
) #main panel
) #sidebarlayout
) #fluidpage
server <- function(input, output, session) {
datasetInput <- reactive({
infile <- input$FileInput
if (is.null(infile))
return(NULL)
read.csv(infile$datapath, header = TRUE)
})
output$table = DT::renderDataTable(datasetInput())
output$selector <- renderUI({
choices <- NULL
if(!is.null(datasetInput()))
choices <- names(datasetInput())
wired_select(inputId = "responsevar",
label = "Dependent Varibale:",
choices = choices)
})
} #server
Key differences to my original answer, are uiOutput replacing selectInput and a renderUI output component instead of an observer.
Related
I want to be able to design a set of UI that can call R's built-in functions, but the current problem lies in the data selection when calling the function. I need to return the calculation result of a function to my UI interface so that another function can be arbitrary Select the variables in the current environment as the input of the function. This is my current code. Can anyone give me some suggestions or some cases?
library(shiny)
function_choose = c("sin","cos")
ui <- fluidPage(
selectInput('f', 'function_choose', function_choose,
selected = function_choose[[1]]),
sidebarPanel(
conditionalPanel(condition = "input.f=='sin'",
mainPanel(
selectInput('sin_dat','data',c("I want to show all the variables in the workspace here ")),
actionButton(inputId = "sin_run",label = "RUN")
)
),
conditionalPanel(condition = "input.f=='cos'",
mainPanel(
selectInput('workspace','data',c("I want to show all the variables in the workspace here "))),
actionButton(inputId = "cos_run",label = "RUN")
)
),
mainPanel(
textOutput("text")
)
)
server <- function(input, output,session) {
data <- c(0.1,0.2,0.3)
observeEvent(input$sin_run,{
data_sined <- sin(data)
output$text <- renderText({
"data_sined is created"
})
})
observeEvent(input$cos_run,{
data_cosed<- cos(data)
output$text <- renderText({
"data_cosed is created"
})
})
}
shinyApp(ui = ui, server = server)
I changed the logic of your app to make it less complicated, I hope this is still ok for your purpose. We can use ls() to get a character vector of all variables in the global (or any other) environment. If we define a vector function_choose in the global environment, then this will be available too. We could easily circumvent this by defining the choices argument inside selectInput or by specifying a names pattern that is selected by ls(). Once we have selected a variable, the input$data returns a character vector. To access the underyling data based on a character vector we use get().
library(shiny)
function_choose = c("sin","cos")
shinyApp(
ui = fluidPage(
sidebarPanel(
selectInput('f', 'function_choose', choices = function_choose,
selected = function_choose[1]),
selectInput('data','data', choices = ls()),
actionButton(inputId = "run",label = "RUN")
),
mainPanel(
textOutput("text")
)
),
server = function(input, output,session) {
res <- eventReactive(input$run, {
dat <- get(input$data)
switch(input$f,
sin = sin(dat),
cos = cos(dat)
)
})
output$text <- renderText({
res()
})
})
I am trying to write a script in shiny, which has two inputs and stores the inputs in two different variables and runs a code using these input variables.But i am getting an error which says :Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
The following is my ui code:
ui <- fluidPage(
titlePanel("Network Model"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "origin",
label = "Origin:",
choices = milk_runs$Origin),
selectInput(inputId = "destination",
label = "Destination:",
choices = milk_runs$Dest),
actionButton("go", "")
),
mainPanel(
tableOutput(
"view"))
)
)
server code :
server<- function(input, output){
origin <- input$origin
destination <- input$destination
observeEvent(input$go,source("nr9.R"))
output$summary <- renderPrint({
#dataset <- datasetInput()
summary(Tnetwork)
})
Can you please tell me how to get correct results.
I think (it would help if you provided a fully reproducible example) that the error is occurring because you are trying to run input$origin without reactive(). The input$origin will not invalidate and update based on user input unless put inside reactive. Based on the example you provided:
library(shiny)
ui <- fluidPage(
titlePanel("Network Model"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "origin", label = "Origin:", choices = c("A","B","C","D","E","F")),
selectInput(inputId = "destination", label = "Destination:", choices = c("A","B","C","D","E","F")),
actionButton("go", "GO")
),
mainPanel( tableOutput( "view"))
)
)
server<- function(input, output){
origin <- reactive(input$origin)
destination<-reactive(input$destination)
observeEvent(input$go,{
cat(origin(),'nextword',destination(),sep="-")
})
output$view <- renderTable({data.frame(origin=origin(),destination=destination())})
}
shinyApp(ui, server)
should print 'origin-nextword-destination' to the console when 'go' is activated, and the table should update. I changed a few bits in your example because it was not reproducible but hopefully it helps.
I have a Shiny app that includes a number of dropdown selection boxes, the values of which are filled from reading an RDS file. The app also includes a fileInput function to upload new data. How can I change the values in the dropdown boxes to reflect the new data? Currently I can see that the data is uploaded, but the old data remains in the dropdown.
The data that should be uploaded is saved to a file using
saveRDS( data.frame(names=c("Jill","Jane","Megan")),"myDataFrame.rds")
In my app.R file, I first define the 'default' value of the data:
myDataFrame <- data.frame(names=c("Tom","Dick","Harry"))
The content of my app.R is as follows:
library(shiny)
ui <- shinyUI(
fluidPage(
fileInput('file1', 'Choose file to upload',accept = ".rds"),
selectInput("myNames","Names",myDataFrame$names),
tableOutput('contents')
)
)
server <- shinyServer(function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile)) { return(myDataFrame) }
readRDS(inFile$datapath)
})
})
The initial view of the application is as expected: both the dropdown and the table contain the 'default' names. Upon upload of my RDS file containing a new dataframe, the table changes (which is what I was looking for) but the dropdown values do not. How can I make the latter happen?
I added reactive object myData that you have to use for table contents, but more importantly to update choices in selectInput (check observe and updateSelectInput part).
library(shiny)
ui <- shinyUI(
fluidPage(
fileInput("file1", "Choose file to upload", accept = ".rds"),
selectInput("myNames","Names", ""),
tableOutput("contents")
)
)
server <- function(input, output, session) {
myData <- reactive({
inFile <- input$file1
if (is.null(inFile)) {
d <- myDataFrame
} else {
d <- readRDS(inFile$datapath)
}
d
})
output$contents <- renderTable({
myData()
})
observe({
updateSelectInput(session, "myNames",
label = "myNames",
choices = myData()$names,
selected = myData()$names[1])
})
}
shinyApp(ui, server)
to riff off of #PoGibas' answer, I needed to load multiple list values for an app, here is a similar application using reactiveValues and observeEvent :
library(shiny)
# save a dummy RDS for loading
saveRDS(list(names=LETTERS,numbers=seq(10)),'dummy.rds')
# define initial values
myDataList <- list(names=c("Tom","Dick","Harry"), numbers=seq(5))
ui <- shinyUI(
fluidPage(
fileInput("file1", "Choose file to upload", accept = ".rds"),
selectInput("myNames","Names", ""),
selectInput("myNumbers","Numbers", ""),
tableOutput("contents")
)
)
server <- function(input, output, session) {
md <- reactiveValues(
names = myDataList$names,
numbers = myDataList$numbers
)
observeEvent(input$file1,{
d <- readRDS(input$file1$datapath)
for (n in names(d)){
md[[n]] <- d[[n]]
}
})
output$contents <- renderTable({
data.frame(data = c(md$names,md$numbers))
})
observe({
updateSelectInput(session, "myNames",
label = "myNames",
choices = md$names,
selected = md$names[1])
updateSelectInput(session, "myNumbers",
label = "myNumbers",
choices = md$numbers,
selected = md$numbers[1])
})
}
shinyApp(ui, server)
I am trying to build a shiny app where I can upload a csv file and based on the column names, from selectInput - app shows basic statistic (graphs, descriptive statistic-average, sd, var .... and some statistical tests).
There is no problem with datasets, which are already in the app. Also no problem with statistics.
Problem is how to make working selectInput with the names of columns from new upload csv file.
Here is my light version of code:
UI:
library(shiny)
shinyUI(fluidPage(
titlePanel("xxx"),
sidebarLayout(
sidebarPanel(
fileInput("csvFile", "Drag cars.csv over here!"),
selectInput("var", "choose variable:", choices= names(data()))
),
mainPanel(
textOutput("average"),
plotOutput("plot")
)
)
))
SERVER:
shinyServer(function(input, output, session) {
data <- reactive({
file1 <- input$csvFile
if (is.null(file1)) {
return()
}
data = read.csv(file=file1$datapath)
data
})
output$average <- renderText({
paste("average is: ", mean(data()[,input$var]))
})
output$plot <- renderPlot({
boxplot(data()[,input$var])
})
})
You will need to use uiOutput() and renderUI() functions.
library(shiny)
ui <- fluidPage(
titlePanel("xxx"),
sidebarLayout(
sidebarPanel(
fileInput("csvFile", "Drag cars.csv over here!"),
uiOutput("var_ui")
),
mainPanel(
textOutput("average"),
plotOutput("plot")
)
)
)
server <- function(input, output, session) {
data <- reactive({
file1 <- input$csvFile
if (is.null(file1)) {
return()
}
data = read.csv(file=file1$datapath)
data
})
output$average <- renderText({
paste("average is: ", mean(data()[,input$var]))
})
output$plot <- renderPlot({
boxplot(data()[,input$var])
})
output$var_ui <- renderUI({
selectInput("var", "choose variable:", choices= names(data()))
})
}
shinyApp(ui, server)
You can also use observe in server. observe does not return anything. Unlike reactive, it responds immediately (and not lazily). It's best used for ip/op operations.
observe({
updateSelectInput(
session,
"var",
choices = names(data())
)
)}
I am trying to gather user input given a data set. I want to insert a column where the user can determine whether they would want to own one of the cars in the mtdata set. This is completely subjective as opinions differ from person to person so I am not able to program this in. Is there a way to append an extra column that can be a checkbox or dropdown menu to identify cars that a user would "Want to own?
library(shiny)
shinyApp(ui = shinyUI(fluidPage(
titlePanel("Interesting Cars"),
sidebarLayout(
sidebarPanel(
helpText("This is a side bar")),
mainPanel(
tableOutput("view")
)
)
)),
server = function(input, output) {
output$view <- renderTable({
head(mtcars[, 1:4], n = 6)
})
})
How about this, you can use the DT library. By adding the filter option the user can define the different components one wants and see what cars come up.
library(shiny)
library(DT)
shinyApp(ui = shinyUI(fluidPage(
titlePanel("Interesting Cars"),
sidebarLayout(
sidebarPanel(
helpText("This is a side bar")),
mainPanel(
DT::dataTableOutput("view")
)
)
)),
server = function(input, output) {
output$view <- DT::renderDataTable({
datatable(mtcars,
filter = "top"
)
})
})
Edit
If it truly is so important to add another column indicating if it is 'interesting' there will be significantly more code to written if you intend to have users assign it on different conditions. Here is an example with just the mpg. The fundamental idea here is that you assign your data to the reactiveValues function. It can then be modified as you like. This can obviously be improved upon more (as it will continue to add columns) but it demonstrates the concept.
shinyApp(ui = shinyUI(fluidPage(
titlePanel("Interesting Cars"),
sidebarLayout(
sidebarPanel(
helpText("This is a side bar"),
uiOutput("mpg"),
actionButton("add_label", "Mark Interesting")
),
mainPanel(
DT::dataTableOutput("view")
)
)
)),
server = function(input, output) {
values <- reactiveValues(
mydata = mtcars
)
output$mpg <- renderUI({
numericInput("mpg_input", "MPG Cutoff?",
value = 15
)
})
output$view <- DT::renderDataTable({
datatable(values$mydata
)
})
observeEvent(input$add_label, {
validate(
need(!is.null(input$mpg_input), "need mpg value")
)
values$mydata <- data.frame(values$mydata,
Interesting_Flag =
ifelse(values$mydata$mpg > input$mpg_input,
"Interesting",
"Not Interesting"))
})
})