How to make a table with formattable in R? - r

I'm trying to make a table in R with formattable but some rows are returning NA and I don't know how to remove them.
In Excel the table looks like this, and this is how I would like it to be:
In R looks like this:
How can I make this table in R looks like in Excel?
Code:
library(readxl)
library(formattable)
indicadores <- read_excel("Dados/Tabela Indicadores.xlsx")
tabela_indicadores <- formattable(indicadores,
align = c("c", "c", "c", "c"))
File that I'm using: https://drive.google.com/file/d/1CDhMkW7l78WEj45mYZ86LTY6PjCyhsSd/view?usp=sharing

The easiest way to do this is to replace the missing values with a character, "-" or "":
indicadores$Tema <- ifelse(is.na(indicadores$Tema), "-", indicadores$Tema)
tabela_indicadores <- formattable(indicadores, align = c("c", "c", "c", "c"))
I am assuming you do not want to exclude the lines completely. If you do want to remove them, use
tabela_indicadores <- formattable(na.omit(indicadores), align = c("c", "c", "c", "c"))

Related

R Shiny DataTables with column filters - preserve the styling

I use the DT package in Shiny and want to use filters, however I have a problem with the styling of the filters:
Code: DT::datatable(overview,editable=TRUE, rownames=FALSE, selection='none', filter='top')
This is the way my DT looks when I add items. When I leave the dropdown, it looks like the following picture (collapsed) which is of course pretty bad.
I want to preserve the styling with the grey box and not have anything like "[..." for my users. How can I do this? If that is not possible with DT, using another package is also an option for me.
Edit: Reproduceable example
df <- data.frame(a = c("a", "a", "b", "b", "c", "c"), b = c(1,2,3,4,5,6), c("A", "B"))
df <- setNames(df, c("A", "B"))
DT::datatable(df,editable=TRUE, rownames=FALSE, selection='none', filter='top')
Thank you!

replacement has length zero on ave function in R

im working on changing the name of several pictures, actually the the length(filename)=length(name) and both are aprox 1.4million entries, the name_idx have about 350,000 entries
filename <- c("1.jpg", "2.jpg" "3.jpg", ...., )
name <- c("a", "b", "c", "c", "a", "b",....,)
name_idx <- c("a", "b", "c", "d", ..., )
i want to create another list which will contain the new name of the picture, the first entry would be a.1.jpg and the 5th would be a.2.jpg, the 4th would be c.2.jpg and so on
someone in stackoverflow giveme this solution
paste0(name, ".", ave(name, name, FUN = seq_along), ".jpg")
but im getting this error
Error in x[...] <- m : replacement has length zero
i was hoping you can helpme to know why is this and how to solve it?
i finally done it in python, whit the glob and the os libraries =3

dplyr text mining Column `text` must be a 1d atomic vector or a list

I'm experimenting with Tidytext (Text Mining with R) and I want to use the function pairwise_count from the widyr library. My Input looks like:
my input table
This is may code in the RapidMiner Execute R operator:
rm_main = function(data)
{
library(dplyr)
library(tidytext)
library(widyr)
set.seed(2017)
test <- data_frame(data) %>%
pairwise_count(text, word)
print(data)
return(list(data))
}
When I execute the code I get the Error:
"Column data must be a 1d atomic vector or a list"
Is there maybe someone who can help me, please.
Regards
Tobias
While I wait for details on your data set, I have noticed few errors in your code. e.g.:
If data parameter to function rm_main is data.frame then there is no need to convert it to data.frame.
library can be loaded at the very beginning of your code.
The parameters to function pairwise_count were not correct. The parameter should match the column name in data.frame or data.table
After correction, the function and its usage on a sample data will be:
library(dplyr)
library(tidytext)
library(widyr)
df <- data_frame(Id = rep(1:5, each = 2),
Text = c("a", "b",
"a", "c",
"a", "c",
"b", "e",
"b", "f"))
#define function
rm_main = function(data)
{
if(is.data.frame(data)){
test <- data %>%
pairwise_count(Text, Id)
}
print(data)
return(list(data))
}
#call function now
rm_main(df)

Kable and pandoc.table takes only the first six rows

I'm trying to build a table either using pandoc.table or kable and have problems getting them to print all 10 rows in my table, atm they both only prints the first six. While I moved to write the table manually, which works, it would be nice to know what's wrong with my code. I haven't seen anything to suggest that 6 rows are the limit, so my code should be workning? Anyone know why it doesn't? If I subset the dt I can print the last 4 as well so maybe 6 rows are a limit. Code below:
library("data.table")
library("knitr")
library("pander")
count.mark <- 35
dt.tbl1 <- data.table(Var = c("Geo", "A", "A",
"Cust", "A",
"Ins", "A",
"Vei", "A",
"Brand"),
RangeR = c("A1", "S1", "T1",
"Com", "Pri",
"T", "B",
"Pa", "Pe",
paste("A1 - A99 (",
count.mark, ")", sep="")
)
)
pandoc.table(head(dt.tbl1), justify = c("left", "centre"))
kable(head(dt.tbl1), justify = c("left", "centre"))
That's because you're using head(dt.tbl1), which by default shows the first six rows. You should just do, e.g.
pandoc.table(dt.tbl1, justify = c("left", "centre"))

How to define if else statement whereby the else statement can remove unwanted \t

I have a function, where one part reads as follows:
conefor.input <- function(conefor.file, onlyoverall, probmin, index)
{
for(i in 1:l00)
{
cat(paste(conefor.file,
if(onlyoverall=="TRUE")
{onlyoverall<-"onlyoverall"},
distance,
if(probmin=="TRUE")
{probmin<-paste("-pcHeur", min)},
index, sep="\t"),file="conef_command.txt", sep="\n")}
return("conef_command.txt")
}
My objective is to generate 100 lines where each input is divided by a tab.
There is no problem when the two 'if' statements above are true. With the resulting being:
conefor.file\tonlyoverall\tdistance\tpcHeur min\tindex
However, when the two 'if' statements above are false, I am left with two tabs where 'onlyoverall' and 'pcHeur' would have been, whereas, what I really want is no action at all, and only one tab separating each argument.
Example when 'if' statements are false:
conefor.file\t\tdistance\t\tindex
What I want when 'if' statements are false:
conefor.file\tdistance\tindex
Many thanks in advance
it's hard to suggest any form of vectorization or similar for your for loop, since it's not clear (to me at least)
what differences there are between iterations.
However, as for the paste statement inside of cat, you can use the following instead
paste(ifelse(onlyoverall,
paste(conefor.file, onlyoverall<-"onlyoverall", sep="\t"),
conefor.file),
ifelse(probmin,
paste(distance, probmin<-paste("-pcHeur", min), sep="\t"),
distance),
index,
sep="\t")
Notice, this grabs the output prior to the if statement and includes in an ifelse output, where it is given for both TRUE and FALSE
eg:
x1 <- x2 <- TRUE
paste(ifelse(x1, paste("A", "B" , sep="#"), "A"), ifelse(x2, paste("C", "D", sep="#"), "C"), "E", sep="#")
# [1] "A#B#C#D#E"
x1 <- x2 <- FALSE
paste(ifelse(x1, paste("A", "B" , sep="#"), "A"), ifelse(x2, paste("C", "D", sep="#"), "C"), "E", sep="#")
# [1] "A#C#E"
x2 <- TRUE
paste(ifelse(x1, paste("A", "B" , sep="#"), "A"), ifelse(x2, paste("C", "D", sep="#"), "C"), "E", sep="#")
# [1] "A#C#D#E"

Resources