I'd like to load a table from a file, make some computations (ie. sum elements of two columns) when I click a button and show the results into a datatable. Easy. However, every time I load a new file, I'd like to clean the previous results and not show them, otherwise, it is confusing whether they are the results of the new or the old ones.
Here's what I tried. but I didn't succeeed on it...
example table: tab.csv
x;A;B
x1;1;0
x2;2;1
x3;1;1
x4;5;2
x5;3;3
code: ui.R
shinyUI(pageWithSidebar(
headerPanel(""),
sidebarPanel(),
mainPanel(fluidRow(
fileInput("table", "Choose CSV File"),
actionButton("BUTCS", "Compute sum"),
dataTableOutput("tablesum")
))
))
server.R
shinyServer(function(input, output) {
user <- new.env()
user$table <- NULL
user$tablesum <- NULL
observe({
if(is.null(input$table)){return()}
tablefilecsv <- input$table
user$table <- read.csv2(tablefilecsv$name, header = TRUE)
})
observeEvent(input$table, {
if(is.null(input$table)){return()}
user$tablesum <- NULL
})
output$tablesum <- renderDataTable(
{
if(is.null(input$BUTCS)){return()}
d <- user$table
user$tablesum <- data.frame(x=d$x, sum=(d$A+d$B))
}, options = list(paging = FALSE,searching = FALSE))
})
Try, i think it is what you want
shinyServer(function(input, output) {
user <- reactiveValues(table= NULL, tablesum= NULL)
observeEvent(input$table, {
if(is.null(input$table)){
return()
}else{
tablefilecsv <- input$table
user$table <- read.csv2(tablefilecsv$datapath ,header = TRUE)
output$tablesum <- renderDataTable(NULL)
}
})
observeEvent(input$BUTCS,{
output$tablesum <- renderDataTable({
d <- user$table
user$tablesum <- data.frame(x=d$x, sum=(d$A+d$B))
}, options = list(paging = FALSE,searching = FALSE))
})
})
Option using reactive functional ( added by #Stefano)
shinyServer(function(input, output) {
data <- reactive({
tablefilecsv <- input$table
table <- read.csv2(tablefilecsv$name, header=TRUE)
})
observeEvent(input$table,{
output$tablesum <- renderDataTable(NULL)
})
observeEvent(input$BUTCS,{
output$tablesum <- renderDataTable({
d <- data()
tablesum <- cbind.data.frame(x=d$x, sum=(d$A+d$B))
}, options = list(paging=FALSE, searching=FALSE))
})
})
Related
I am building a Shiny app and using the code from this question as an example: How to download editable data table in shiny. However, in my code the df <- reactiveVal(dat) does not work, because the dat itself is already a reactive value that comes from an eventReactive({}) function. This is the code I am working with, it works if I define the dat outside of the server, but not when it is created inside the server function of shiny. How do I make a copy of it so that I can show it in a new table (and potentially process further and download in later steps in the app)?
library(shiny)
library(DT)
library(shinyWidgets)
# if the data frame is just an object, it works
#dat <- iris[1:3, ]
ui <- fluidPage( actionBttn(
inputId = "btnProcess",
label = "Process",
size = "sm",
color = "success"
),
DTOutput("my_table"),
DTOutput("table2")
)
server <- function(input, output){
# if the dataframe is a reactive variable, this doesnt work.
dat <- eventReactive(input$btnProcess, {
iris[1:3, ]
})
output[["my_table"]] <- renderDT({
datatable(dat(), editable = "cell")
})
#############################
#### none of these work #####
#############################
#df <- reactiveVal(dat)
#df <- reactiveVal(dat())
#df <- dat()
#df <- dat
observeEvent(input[["my_table_cell_edit"]], {
cell <- input[["my_table_cell_edit"]]
newdf <- df()
newdf[cell$row, cell$col] <- cell$value
df(newdf)
})
output[["table2"]] <- renderDT({
datatable(df())
})
}
shinyApp(ui, server)
Try this
ui <- fluidPage( actionBttn(
inputId = "btnProcess",
label = "Process",
size = "sm",
color = "success"
),
actionBttn(inputId = "reset", label = "Reset", size="sm", color="warning"),
DTOutput("mytable"),
DTOutput("table2")
)
server <- function(input, output){
# if the dataframe is a reactive variable, this doesnt work.
dat <- eventReactive(input$btnProcess, {
iris[1:3, ]
})
mydf <- reactiveValues(data=NULL)
observe({
mydf$data <- dat()
})
output$mytable <- renderDT({
datatable(mydf$data, editable = "cell")
})
observeEvent(input$mytable_cell_edit, {
info = input$mytable_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
mydf$data[i, j] <<- DT::coerceValue(v, mydf$data[i, j])
})
output[["table2"]] <- renderDT({
datatable(mydf$data)
})
observeEvent(input$reset, {
mydf$data <- dat() ## reset it to original data
})
}
shinyApp(ui, server)
I have a running example: I am updating a data.table depending on users input via checkboxes. So far Iam filtering the data explicitly, but I would like to do that with the help of a loop using a for loop or a function of the apply-family. Unfortunately I cannot get either to work.
library(shiny)
library(data.table)
library(DT)
tdata <- data.table(fruit = c(rep("Apple",4),rep( "Ban",4)),
bug1 = c(rep(c("+","+", "-","-"),2)),
bug2 = c(rep(c("+","-"),4)),
value = c(rep(c(0.25),4), 0.6,0.4,0,0))
ui <- (fluidPage(tagList(
sidebarLayout(
sidebarPanel(uiOutput("file_input")),
mainPanel(dataTableOutput('fruit_table'))
))))
server <- function(input, output) {
fileData <- reactive(
return(tdata)
)
colname_list <- reactive(
colnames(fileData())
)
output$file_input <- renderUI ({
if(is.null(fileData())){
return()
}else{
tagList(
lapply(1:(length(fileData())-1), function(i){
choice_list = unique(fileData()[,get(colnames(fileData()[,i, with = FALSE]))])
checkboxGroupInput(inputId = colnames(fileData()[,i, with = FALSE]),
label = colnames(fileData()[,i, with = FALSE]),
choices = choice_list,
inline = TRUE,
selected = fileData()[1, i, with = FALSE])
})
)
}
})
# works fine, but usually the number of columns changes so I want to keep it flexible
fruitFilter <- reactive({
fileData()[[paste0(colname_list()[1])]] %in% input[[paste0(colname_list()[1])]] &
fileData()[[paste0(colname_list()[2])]] %in% input[[paste0(colname_list()[2])]] &
fileData()[[paste0(colname_list()[3])]] %in% input[[paste0(colname_list()[3])]]
})
# fruitFilter <- reactive({
# for(i in 1: ((length(fileData()))-1)){
# fileData()[[paste0(colname_list()[i])]] %in% input[[paste0(colname_list()[i])]]
# }
# })
output$fruit_table <- renderDataTable({
datatable(fileData()[fruitFilter(),])
})
}
shinyApp(ui = ui, server = server)
I still consider myself a newby to Shiny. I appreciate any help! Thanks.
In the loop approach, we could initialize a list and then Reduce the output to a single logical vector
fruitFilter <- reactive({
i1 <- head(seq_along(fileData()), -1)
l1 <- vector('list', length(i1))
for(i in i1){
l1[[i]] <- fileData()[[colname_list()[i]]] %in% input[[colname_list()[i]]]
}
Reduce(`&`, l1)
})
With the full code
library(shiny)
library(data.table)
library(DT)
tdata <- data.table(fruit = c(rep("Apple",4),rep( "Ban",4)),
bug1 = c(rep(c("+","+", "-","-"),2)),
bug2 = c(rep(c("+","-"),4)),
value = c(rep(c(0.25),4), 0.6,0.4,0,0))
ui <- (fluidPage(tagList(
sidebarLayout(
sidebarPanel(uiOutput("file_input")),
mainPanel(dataTableOutput('fruit_table'))
))))
server <- function(input, output) {
fileData <- reactive(
return(tdata)
)
colname_list <- reactive(
colnames(fileData())
)
output$file_input <- renderUI ({
if(is.null(fileData())){
return()
}else{
tagList(
lapply(1:(length(fileData())-1), function(i){
choice_list = unique(fileData()[,get(colnames(fileData()[,i, with = FALSE]))])
checkboxGroupInput(inputId = colnames(fileData()[,i, with = FALSE]),
label = colnames(fileData()[,i, with = FALSE]),
choices = choice_list,
inline = TRUE,
selected = fileData()[1, i, with = FALSE])
})
)
}
})
fruitFilter <- reactive({
i1 <- head(seq_along(fileData()), -1)
l1 <- vector('list', length(i1))
for(i in i1){
l1[[i]] <- fileData()[[colname_list()[i]]] %in% input[[colname_list()[i]]]
}
Reduce(`&`, l1)
})
output$fruit_table <- renderDataTable({
datatable(fileData()[fruitFilter()])
})
}
shinyApp(ui = ui, server = server)
--output
I want to make my rhandsontable read only on clicking the action button 'Freeze Forecast' and activate the table on clicking on 'Edit Forecast'. It should show me the Sum Output on clicking on 'Generate Forecast' button.
Please help to correct my existing code as per above conditions.
UI.R
packages <- c( "shiny", "data.table", "devtools", "shinysky","googleVis","scales","rhandsontable" )
lapply( packages, require, character.only = TRUE )
jsResetCode <- "shinyjs.reset = function() {history.go(0)}" #JS Code to refresh the App
did_recalc <- FALSE
ui <- fluidPage(
# Application title
titlePanel("Scenario Planner Test App"),
br(),br(),
actionButton("recalc", "Generate Forecast"),
actionButton("edit", "Edit Forecast"),
actionButton("freeze", "Freeze Forecast"),br(),br(),
rHandsontableOutput('table'),br(),br(),
textOutput('restitle'),
textOutput('result')
)
Server.R
Sys.setenv(R_ZIPCMD="/usr/bin/zip")
packages <- c( "shiny", "data.table", "devtools", "shinysky","googleVis","scales","reshape2" )
lapply( packages, require, character.only = TRUE )
disableActionButton <- function(id,session) {
session$sendCustomMessage(type="jsCode1",
list(code= paste("$('#",id,"').prop('disabled',true)"
,sep="")))
}
enableActionButton <- function(id,session) {
session$sendCustomMessage(type="jsCode2",
list(code= paste("$('#",id,"').prop('disabled',false)"
,sep="")))
}
shiny::shinyServer( 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)
})
observe({
input$freeze
print("freeze")
##if(!is.null(input$table))
print("2freeze")
rhandsontable(values$data) %>%
hot_table(readOnly = TRUE)
})
output$restitle <- renderText({
"Sum Output"
})
output$result <- renderText({
sum(values$data)
})
})
)
I got this to work by
Adding a state variable to your reactive called readonly
Adding two observerEvent routines to the edit and freeze action buttions to toggle readonly.
Modifying your output$table command to use the reactive readonly variable.
It would have been easier, and not needed a lot of those elements if you had just used a checkbox to indicate that the table is editable, and then wired that variable to the readOnly parameter, but sometimes you need to do it this way, so I solved it like this.
The complete server.R code is here:
packages <- c("shiny","data.table","devtools","shinysky","googleVis","scales","reshape2")
lapply(packages,require,character.only = TRUE)
disableActionButton <- function(id,session) {
session$sendCustomMessage(type = "jsCode1",
list(code = paste("$('#",id,"').prop('disabled',true)"
,sep = "")))
}
enableActionButton <- function(id,session) {
session$sendCustomMessage(type = "jsCode2",
list(code = paste("$('#",id,"').prop('disabled',false)"
,sep = "")))
}
shiny::shinyServer(function(input,output,session)({
values <- reactiveValues(data = as.data.frame(runif(2)),readonly=FALSE)
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,readOnly=values$readonly)
})
observeEvent(input$edit, {
values$readonly <- FALSE
})
observeEvent(input$freeze,{
values$readonly <- TRUE
})
output$restitle <- renderText({
"Sum Output"
})
output$result <- renderText({
sum(values$data)
})
})
)
and it looks like this:
I am running an example from here.
library(rhandsontable)
library(shiny)
runApp(shinyApp(
ui = fluidPage(rHandsontableOutput("hot")),
server = function(input, output, session) {
fname <- "mtcars2.csv"
values <- reactiveValues()
setHot <- function(x) values[["hot"]] = x
observe({
if(!is.null(values[["hot"]])) write.csv(values[["hot"]], fname)
})
output$hot <- renderRHandsontable({
if (!is.null(input$hot)) {
DF <- hot_to_r(input$hot)
} else {
DF <- read.csv("mtcars.csv", stringsAsFactors = FALSE)
}
setHot(DF)
rhandsontable(DF) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
hot_cols(columnSorting = TRUE)
})
}
))
I want changes made to table be saved in file mtcars2.csv. I also want to preserve row order. In project home page it says "sorting only impacts the widget and will not reorder the original data set". Can I somehow get current view of a table and save it?
The best way to answer this question will be to file an issue at https://github.com/jrowen/rhandsontable. Currently, these lines define only a partial list of handsontable events. This list does not include afterColumnSort which would be what you need. Here is a quick hack to partially answer your question.
library(rhandsontable)
library(shiny)
library(htmlwidgets)
runApp(shinyApp(
ui = fluidPage(
rHandsontableOutput("hot"),
tags$script(
'
setTimeout(
function() {
HTMLWidgets.find("#hot").hot.addHook(
"afterColumnSort",
function(){
console.log("sort",this);
Shiny.onInputChange(
"hot_sort",
{
data: this.getData()
}
)
}
)
},
1000
)
'
)
),
server = function(input, output, session) {
observeEvent(
input$hot_sort
,{
print(input$hot_sort$data)
}
)
output$hot <- renderRHandsontable({
if (!is.null(input$hot)) {
DF <- hot_to_r(input$hot)
} else {
DF <- mtcars
}
rhandsontable(DF) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
hot_cols(columnSorting = TRUE)
})
}
))
I don't think there is a way to preserve the sorted columns in DataTables for shiny, Sad!
With the below code I'm able to save changes made in shiny app to the file mtcars2.csv. Interestingly! post sorting by desired column, clicking on any data cell and pressing enter key saves the row order to the mtcars2.csv. Agree with timelyportolio's point on filing an issue on git.
R Code:
library(shiny)
library(rhandsontable)
runApp(shinyApp(
ui = fluidPage(titlePanel("Edit Data File"),
helpText("Changes to the table will be automatically saved to the source file."),
# actionButton("saveBtn", "Save"),
rHandsontableOutput("hot")),
shinyServer(function(input, output, session) {
values = reactiveValues()
data = reactive({
if (is.null(input$hot)) {
hot = read.csv("mtcars.csv", stringsAsFactors = FALSE)
} else {
hot = hot_to_r(input$hot)
}
# this would be used as a function input
values[["hot"]] = hot
hot
})
observe({
# input$saveBtn
if (!is.null(values[["hot"]])) {
write.csv(values[["hot"]], "mtcars.csv", row.names = FALSE)
}
})
output$hot <- renderRHandsontable({
hot = data()
if (!is.null(hot)) {
hot = rhandsontable(hot) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
hot_cols(columnSorting = TRUE)
hot
}
})
})
))
I am using rhandsontable in a Shiny App and I would like to know how to use the getSelected() method of Handsontable in this case, as I intend to apply changes on the data.frame.
thank you!
You can obtain the selected row, column, range, and cell values, as well as the edited cells using selectCallback=TRUE. You can edit a cell by double-clicking on it, and accept the changes by pressing "return" or "enter".
Minimal example:
library(shiny)
library(rhandsontable)
ui=fluidPage(
rHandsontableOutput('table'),
verbatimTextOutput('selected')
)
server=function(input,output,session)({
df=data.frame(N=c(1:10),L=LETTERS[1:10],M=LETTERS[11:20])
output$table=renderRHandsontable(
rhandsontable(df,selectCallback = TRUE,readOnly = FALSE)
)
output$selected=renderPrint({
cat('Selected Row:',input$table_select$select$r)
cat('\nSelected Column:',input$table_select$select$c)
cat('\nSelected Cell Value:',
input$table_select$data[[
input$table_select$select$r]][[input$table_select$select$c]])
cat('\nSelected Range: R',input$table_select$select$r,
'C',input$table_select$select$c,':R',input$table_select$select$r2,
'C',input$table_select$select$c2,sep="")
cat('\nChanged Cell Row Column:',input$table$changes$changes[[1]][[1]],
input$table$changes$changes[[1]][[2]])
cat('\nChanged Cell Old Value:',input$table$changes$changes[[1]][[3]])
cat('\nChanged Cell New Value:',input$table$changes$changes[[1]][[4]])
})
}) # end server
shinyApp(ui = ui, server = server)
While rhandsontable is a real good implementation of handsontable (credit goes to #jrowen), currently it does not include getSelected().
The event of a user altering any cell (including selecting / deselecting a checkbox) is tracked by shiny. This gives the opportunity to use checkboxes to let the user to select (or de-select) one or more rows.
Unfortunately the logic to understand what has been selected needs to be developed on the server side by your code.
The snippet of code below may give you some idea on how to manage it.
options(warn=-1)
library(rhandsontable)
library(shiny)
options(warn=-1)
quantity <- id <- 1:20
label <- paste0("lab","-",quantity)
pick <- FALSE
iris_ <- data.frame(id=id,pick=pick, quantity=quantity,label=label,iris[1:20,] ,stringsAsFactors = FALSE)
mtcars_ <- data.frame(id=id,pick=pick, quantity=quantity,label=label,mtcars[1:20,] ,stringsAsFactors = FALSE)
iris_$Species <- NULL # i.e. no factors
#---------------------------
ui <- fluidPage(
fluidRow(
column(6,rHandsontableOutput('demTb')),
column(3,uiOutput("demSli")),
column(3, radioButtons("inButtn", label=NULL, choices= c("iris","mtcars"), selected = "iris", inline = TRUE))
)
)
server <- function(session, input, output) {
selData <- ""
output$demSli <- renderUI({
if(is.null(input$demTb) ) return()
isolate({
df_ <- hot_to_r(input$demTb)
index <- which(df_$pick==T)
if(length(index)==0) return()
labs <- iris_$label[index]
pages <- "test"
iter <- length(labs)
buttn <- 1
valLabs <- sapply(1:iter, function(i) {
if(is.null(input[[paste0(pages,"d",labs[i],buttn)]] )) {
0
} else { as.numeric(input[[paste0(pages,"d",labs[i],buttn)]]) }
})
#
toRender <- lapply(1:iter, function(i) {
sliderInput(inputId = paste0(pages,"d",labs[i],buttn),
label = h6(paste0(labs[i],"")),
min = -100,
max = 100,
step = 1,
value = valLabs[i],
post="%",
ticks = FALSE, animate = FALSE)
})
})
return(toRender)
})
#--------------------
rds <- reactive({
# if( is.null(input$demTb) ) {
if( input$inButtn == "iris") {
if(selData == "" | selData == "mtcars") {
selData <<- "iris"
return(iris_) # first time for iris
}
} else {
if(selData == "iris" ) {
selData <<- "mtcars"
return(mtcars_) # first time for mtcars
}
}
df_ <- hot_to_r(input$demTb)
isolate({
index <- which(df_$pick==T)
if(length(index)==0) return(df_)
labs <- iris_$label[index]
pages <- "test"
iter <- length(labs)
buttn <- 1
}) # end isolate
valLabs <- sapply(1:iter, function(i) {
if(is.null(input[[paste0(pages,"d",labs[i],buttn)]] )) {
0
} else {
as.numeric(input[[paste0(pages,"d",labs[i],buttn)]])/100
}
})
dft_ <- data.frame(label=labs, multi=valLabs, stringsAsFactors = FALSE)
dft_ <- merge(iris_,dft_,by="label", all.x=T)
dft_$quantity <- sapply(1:length(dft_$quantity), function(z) {
if( is.na( dft_$multi[z]) ) {
dft_$quantity[z]
} else { iris_$quantity[z]*(1 + dft_$multi[z]) }
})
dft_[with(dft_,order(as.numeric(id))),]
df_[with(df_,order(as.numeric(id))),]
df_$quantity <- df_$quantity
return(df_)
})
output$demTb <- renderRHandsontable({
if(is.null(rds() )) return()
df_ <- rds()
df_ <- df_[with(df_,order(as.numeric(id))),]
rhandsontable(df_, readOnly = FALSE, rowHeaders= NULL, useTypes= TRUE) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE)
})
}
shinyApp(ui, server)