I am trying to create a shiny app that has a rhandsontable in it. I want rhandsontable to be able to update its values in one of its columns if the corresponding values in another column is selected/ checked. So far, I have been able to use reactive / observe events to change the output values between two objects but i am unable to wrap my head around it , i.e, how do i make once column of rhandsontable reactive to another column in the same table ?
Here is a simple example of what i am trying to build:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
rHandsontableOutput('table')
)
server <- function(input,output,session)({
data <- data.frame(c1=c(5,10,15), c2=c(3,6,9) , diff=c(0,0,0), select= as.logical( c(FALSE,FALSE,FALSE)))
output$table <- renderRHandsontable({
rhandsontable(data)
})
})
shinyApp(ui = ui, server = server)
So if i check the column 'Select', column 'diff' should produce the difference between column c1 & c2
From what I understand, your goal is to do some calculation depending on the values of some other column. So if for example a box of the third column is checked, you might want to compute the difference between elements of column 1 and 2.
If you had just a data frame, that would be easy, wouldn't it? Well, this is possible using reactive values. The main idea is that you can store the rhandsontable in a data frame in the backend, modify the data frame and then render the modified data frame once again back in the handsontable.
I hope this helps:
For a more detailed example on reactive values you can see
this: http://stla.github.io/stlapblog/posts/shiny_editTable.html
and this : https://www.youtube.com/watch?v=BzE1JmC0F6Q&list=PL6wLL_RojB5wXR3NR3K38sIvexZ_45alY&index=3
library(rhandsontable)
library(shiny)
ui <- fluidPage(
mainPanel(
rHandsontableOutput("hot")
)
)
server = function(input, output, session){
df<- data.frame(c1=c(5,10,15), c2=c(3,6,9) , diff=c(0,0,0), select= as.logical( c(FALSE,FALSE,FALSE)))
values <- reactiveValues(data = df)
observe({
if(!is.null(input$hot)){
values$data <- as.data.frame(hot_to_r(input$hot))
isolate(values$data[,'diff'] <- ifelse(values$data[,'select'], values$data[,'c1']-values$data[,'c2'] ,0))
print(values$data)
output$hot <- renderRHandsontable({
rhandsontable(values$data)
})
}
})
output$hot <- renderRHandsontable({
rhandsontable(values$data)
})
}
shinyApp(ui, server)
Related
I am trying to get familiar with the rhandsontable package. So I tried something I thought should be pretty easy but I can't find a solution. Here is the idea:
I am creating a dataframe with random numbers and in a text box. The mean of column 1 of the dataframe should be displayed. Furthermore, that number should be updated as soon as I change the value of a cell in the dataframe.
My code:
ui <- fluidPage(
textOutput("num"),
rHandsontableOutput(outputId="frame")
)
server <- function(input, output, session) {
datavalue <- reactiveValues(data=df)
observeEvent(input$frame$changes$changes,{
mean_col1 <- mean(datavalue$data[[1]][1:10])
})
output$num <- renderText({
mean(datavalue$data[[1]][1:10])
})
output$frame <- renderRHandsontable({
rhandsontable(datavalue$data)
})
}
shinyApp(ui = ui, server = server)
I think you want to use hot_to_r to convert the handsontable to an R object when there is a change. You can update your reactiveValue datavalue$data when that happens, and your output$num will account for this change as well with the new mean.
Try using this in your observeEvent:
datavalue$data <- hot_to_r(input$frame)
As an alternative, you can do a general observe as follows:
observe({
req(input$frame)
datavalue$data <- hot_to_r(input$frame)
})
Forgive the non-reproducible example. A theoretical solution will do just fine. I want to know how to subset a dataframe stored in a reactive expression for one particular column, to be be assigned a unique output_id, and ultimately displayed in the UI. This is analogous to accessing a column of a dataframe like so: df$column_name
I store a dataframe as a reactive expression called data(), using eventReactive() which is linked with an actionButton() in the UI.
Code in Environment:
# dataframe with n columns and k rows
df
UI:
actionButton(inputId = "go", label = "Update")
SERVER:
# create a reactive expression 'data()', a subsetted data.frame based on other reactive values
data() <- eventReactive( input$go, { df %>% filter(based on other reactive values) } )
output$output_id <- renderSomething( { code depends on data()$specific column })
May be the following example answers what you are after. The UI has a multi select list, the entries of the lists can be used to subset the Species column of iris data set.
# Using multi select list to sum columns
library(shiny)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Subset and sum a column of iris"),
fluidRow(
selectInput('Species', 'Species', levels(iris$Species), multiple = TRUE, selectize = FALSE)
),
fluidRow(verbatimTextOutput('selection')),
fluidRow(tableOutput('dataColumn')),
fluidRow(
tags$h2("sum:"),
verbatimTextOutput('sum')
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$selection <- reactive(input$Species)
subiris = reactive({
subset(iris, Species %in% input$Species)
})
output$dataColumn <- renderTable(subiris()$Sepal.Length)
output$sum <- renderPrint(sum(subiris()$Sepal.Length))
}
# Run the application
shinyApp(ui = ui, server = server)
I want to create a matrix or table as input for user to interact with in Shiny.
For example:
sample name number of tests
350292 3
... ...
I want to automatically generate tabs in the mainPanel for user to input data for the different samples.
This is possible with matrixInput in the shinyIncubator package, but the matrixInput function does not support column names.
Is there a better way to do this?
Update
I tried the rhandsontable package.
Code:
library(shiny)
library(rhandsontable)
DF <- data.frame(name=c(350292, 360765), run=c(3,2))
colnames(DF) <- c("sample name", "number of tests")
ui <- fluidPage(
headerPanel("test"),
mainPanel(rHandsontableOutput("sample"))
)
server <- function(input, output) {
output$sample <- renderRHandsontable({
rhandsontable(DF, rowHeaders = NULL) %>%
hot_col(c("sample name", "number of tests"), format = "0")
})
}
shinyApp(ui = ui, server = server)
How can I call values using the reactive() and rhandsontable?
I want to create tabs based on sample name and test number.
I have a problem accessing individual values of a data frame that has been created from rhandsontable package. In the following code co[1] retrieves column 1 which is sensible, but then I try to access col[1,1] being row 1 column 1 produces missing value where TRUE/FALSE needed which is a bizarre error in this circumstance. I have tried accessing it with a list method namely co[[[1]][1] with no luck.
library(shiny)
library(rhandsontable)
did_recalc <- FALSE
ui <- fluidPage(
rHandsontableOutput('table'),
dataTableOutput('result'),
br(),
# tableOutput(),
actionButton("recalc", "Return to original values")
)
server <- function(input,output,session)({
a<-c(1,2,4,5,6,7)
b<-c(1,2,4,5,6,7)
c<-rbind(a,b)
df1<-data.frame(c)
#creates reactive values for the data frame
values <- reactiveValues(data=df1)
#if recalc --- which connects to an action button in the ui is hit, values goes back to original data frame
observe({
input$recalc
values$data <- df1
})
#this changes the handsontable format to an r object
observe({
if(!is.null(input$table))
values$data <- hot_to_r(input$table)
})
#important that you are using the values$data that have come from a reactive function
output$table <- renderRHandsontable({
rhandsontable(values$data)
})
fn<-reactive({
co<-data.frame((values$data))
#output$tst<-renderTable(co[3,2]*2) #So if I convert to a new data frame I can do operations on the columns
#Still need to be able to access individual cells
return(co)
})
output$result<-renderDataTable({
fn()
})
})
shinyApp(ui = ui, server = server)
I get the feeling that the object I am trying to access is not clean or structured in the way I am assuming it is.
I am using the rhandsontable package in a Shiny app which should have the following functionality:
the data used in the calculation can be randomly generated, invoked by an actionButton (and when the app starts)
the data can be manually edited by the user via the handsontable object
after manual editing it should be possible to re-generate random data, invoking a new calculation
The following app does exactly that what I want, but I could not figure it out how to get rid of the global variable did_recalc. It is a minimal example, where the data consists of two numeric values which are summed up.
library(shiny)
library(rhandsontable)
did_recalc <- FALSE
ui <- fluidPage(
rHandsontableOutput('table'),
textOutput('result'),
actionButton("recalc", "generate new random vals and calculate")
)
server <- function(input,output,session)({
dataset_generator <- eventReactive(input$recalc, {
df <- as.data.frame(runif(2))
output$table <- renderRHandsontable({rhandsontable(df)})
did_recalc <<- TRUE
df
}, ignoreNULL = FALSE)
output$result <- renderText({
df <- dataset_generator()
if (!is.null(input$table) && !did_recalc)
df <- hot_to_r(input$table)
did_recalc <<- FALSE
sum(df)
})
})
shinyApp(ui = ui, server = server)
If I remove the !did_recalc condition within output$result <- ... then editing the table still invokes a (correct) calculation. But if "recalc" is pressed (after some manual editing was done), then the "recalc" button just generates new random values, but without recalculating the sum.
It seems to me, that input$table can just be changed by manual edits of the table object and does not care about new values given via renderRHandsontable. Hence I need this hack with the global variable, which allows me to track if the user just re-generated the data (causing that input$table is "outdated")
Has anybody an idea how to get the functionality of this example without the global variable?
You could store the data in a reactiveValues and have two observers updating it; one if the button is clicked, one if the table is edited by hand.
In your output$table and output$result, you then just need to use the data that is in the reactiveValues. Here's an example (same ui.R as you posted):
server <- function(input,output,session)({
values <- reactiveValues(data=as.data.frame(runif(2)))
observe({
input$recalc
values$data <- as.data.frame(runif(2))
})
observe({
if(!is.null(input$table))
values$data <- hot_to_r(input$table)
})
output$table <- renderRHandsontable({
rhandsontable(values$data)
})
output$result <- renderText({
sum(values$data)
})
})